<?php
include("config.php");
ini_set('display_errors', 'On');
include("classes/connect.php");
require_once('classes/class.jira.php');
require_once("classes/class.gmail.php");

$config->username= $jirauser;
$config->password= $jirapass;
$config->port= "";
$config->host= $host;

$queryIssue = new Jira($config);
$query->jql='project = '.$project.' and updated > -1h';
$array=$queryIssue->queryMoreIssue($query);
$response=json_decode($array->responseBody);
foreach($response->issues as $key){
   
    /* last updated date can be obtain by $key->fields->updated, but cant get updated user  */
        $last_changed_by = NULL;
        $last_changed_date = NULL;
        
        $comments_log = $queryIssue->queryComment($key->key);
        $change_log = $queryIssue->queryChangelog($key->key);

        
        if($change_log){
            $res = json_decode($change_log->responseBody);
            if(is_array($res->changelog->histories)){
               $last_pop = count($res->changelog->histories) - 1;
               $last_changed_by = $res->changelog->histories[$last_pop]->author->displayName;
               $last_changed_date = strtotime($res->changelog->histories[$last_pop]->created);
               
            }

        }
        
        $comment_stake = json_decode($comments_log->responseBody);
        
        if(is_array($comment_stake->fields->comment->comments)){
            array_reverse($comment_stake->fields->comment->comments);
            
            // change last update state if comment update time is ahead from change log time
            foreach ($comment_stake->fields->comment->comments as $comment)
            if(strtotime($comment->updated) > $last_changed_date){
                $last_changed_by = $comment->author->displayName;
                $last_changed_date = strtotime($comment->updated);
               
            }
        }
        
        
	$sql1="SELECT * from trn_emaildetails WHERE jiraTaskID='".$key->id."'";
	$sqlquery=mysql_query($sql1);
	$row = mysql_fetch_array($sqlquery);
	if(mysql_num_rows($sqlquery)>0){
		$sql="UPDATE trn_emaildetails SET 
			currentStatus='".$key->fields->status->name."',
			currentAssignee='".$key->fields->assignee->name."',
			currentSummary = '".$key->fields->summary."',
			priority = '".$key->fields->priority->name."',
			currentAssigneeName = '".$key->fields->assignee->displayName."',
            modifiedBy = '". $last_changed_by ."',
            modifiedDate = '". $last_changed_date ."'             
			where jiraTaskID='".$key->id."'
			";			
		mysql_query($sql);
		
		if($key->fields->status->name!==$row['currentStatus']){
			$historyid=$key->id;
			$array1=$queryIssue->queryChangelog($historyid);
			$response1 = json_decode($array1->responseBody);
			$reversed_history = array_reverse($response1->changelog->histories);
			
			foreach($reversed_history as $value){
				if($value->items[0]->toString==$key->fields->status->name){
					$modifier=$value->author->name;
					$modifieddate=strtotime($value->created);
					break;
				}			
			}
			switch($key->fields->status->name){
				case $action_one:
					$a=str_replace('{modifier}',$modifier,$action_one_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_two:
					$a=str_replace('{modifier}',$modifier,$action_two_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_three:
					$a=str_replace('{modifier}',$modifier,$action_three_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_four:
					$a=str_replace('{modifier}',$modifier,$action_four_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_five:
					$a=str_replace('{modifier}',$modifier,$action_five_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_six:
					$a=str_replace('{modifier}',$modifier,$action_six_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_seven:
					$a=str_replace('{modifier}',$modifier,$action_seven_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_eight:
					$a=str_replace('{modifier}',$modifier,$action_eight_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_nine:
					$a=str_replace('{modifier}',$modifier,$action_nine_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_ten:
					$a=str_replace('{modifier}',$modifier,$action_ten_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_eleven:
					$a=str_replace('{modifier}',$modifier,$action_eleven_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				case $action_twelve:
					$a=str_replace('{modifier}',$modifier,$action_twelve_des_jira);
					$activitysql = "INSERT INTO trn_activity SET
												mainId = '".$row['id']."',
												status = '".$key->fields->status->name."',
												action = '".$key->fields->status->name."',
												description = '".$a."',
												modifiedBy = '".$modifier."',
												modifiedDate = '".$modifieddate."'
									";
					mysql_query($activitysql);
					break;
				
			}
		}
		if($key->fields->assignee->name!==$row['currentAssignee']){
				
			$historyid=$key->id;
			$array1=$queryIssue->queryChangelog($historyid);
			$response1 = json_decode($array1->responseBody);
			$reversed_history = array_reverse($response1->changelog->histories);
			
			foreach($reversed_history as $value){
				if($value->items[0]->to==$response->issues[0]->fields->assignee->name){
					$modifier=$value->author->name;
					$modifieddate=strtotime($value->created);
				}			
			}
			$assigneedescription="[JIRA] changed Assignee to <b>".$key->fields->assignee->name."</b> by <b>".$modifier."</b>";
			$activitysql = "INSERT INTO trn_activity SET 
											mainId = '".$row['id']."',
											status = '".$key->fields->status->name."',
											action = '".$key->fields->status->name."',
											description = '".$assigneedescription."',
											modifiedBy = '".$modifier."',
											modifiedDate = '".$modifieddate."'
								";
			mysql_query($activitysql);
		}
	}
}


?>